package kata02;

import java.util.Arrays;

public class IterationSubChop {
    int[] items;

    public IterationSubChop (int[] items) {
        this.items = items;
    }

    public int findIndex (int element) {
        int[] subItems = items;
        while (subItems.length > 0) {
            int middle = subItems.length - 1 % 2 == 0 ? (subItems.length - 1) / 2 : subItems.length - 1;
            if (element < subItems[middle]) {
                subItems = Arrays.copyOfRange(subItems, 0, middle);
            } else if (element > subItems[middle]) {
                if (middle + 1 > subItems.length - 1) {
                    return -1;
                }
                subItems = Arrays.copyOfRange(subItems, middle, subItems.length);
            } else {
                return middle;
            }
        }
        return -1;
    }
}
